import axios from 'axios'
import type { AxiosInstance } from 'axios'

import { ElLoading } from 'element-plus'

import type { MyRequestInterceptors, MyRequestConfig } from './type'
import { ILoadingInstance } from 'element-plus/lib/el-loading/src/loading.type'

const DEFAULT_LOADING = true

class MyRequest {
  instance: AxiosInstance
  interceptors?: MyRequestInterceptors
  loading?: ILoadingInstance
  showloading: boolean

  constructor(config: MyRequestConfig) {
    this.instance = axios.create(config)
    this.interceptors = config.interceptors
    this.showloading = config.showloading ?? DEFAULT_LOADING

    // 实例传入的拦截器
    this.instance.interceptors.request.use(
      this.interceptors?.requestInterceptor,
      this.interceptors?.requestInterceptorCatch
    )
    this.instance.interceptors.response.use(
      this.interceptors?.responseInterceptor,
      this.interceptors?.responseInterceptorCatch
    )

    // 添加所有实例的拦截器
    this.instance.interceptors.request.use(
      (config) => {
        // console.log('所有实例都有的拦截器,请求拦截成功')
        if (this.showloading) {
          this.loading = ElLoading.service({
            lock: true,
            text: '正在加载数据...',
            background: 'rgba(0,0,0,0.5)'
          })
        }
        return config
      },
      (err) => {
        // console.log('所有实例都有的拦截器,请求拦截失败')
        return err
      }
    )
    this.instance.interceptors.response.use(
      (res) => {
        // console.log('所有实例都有的拦截器,响应拦截成功')
        // 移除请求loading
        this.loading?.close()
        // setTimeout(() => {
        //   this.loading?.close()
        // }, 1000)
        return res.data
      },
      (err) => {
        // console.log('所有实例都有的拦截器,响应拦截失败')
        this.loading?.close()
        return err
      }
    )
  }

  request<T>(config: MyRequestConfig<T>): Promise<T> {
    return new Promise((resolve, reject) => {
      if (config.interceptors?.requestInterceptor) {
        config = config.interceptors.requestInterceptor(config)
      }

      if (config.showloading === false) {
        this.showloading = config.showloading
      }

      this.instance
        .request<any, T>(config)
        .then((res) => {
          if (config.interceptors?.responseInterceptor) {
            res = config.interceptors.responseInterceptor(res)
          }
          // console.log(res)
          // 将showloading在设置成默认的true,这样不会影响下一个请求
          this.showloading = DEFAULT_LOADING
          resolve(res)
        })
        .catch((err) => {
          // 将showloading在设置成默认的true,
          this.showloading = DEFAULT_LOADING
          reject(err)
        })
    })
  }

  get<T>(config: MyRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'GET' })
  }
  post<T>(config: MyRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'POST' })
  }
  delete<T>(config: MyRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'DELETE' })
  }
  patch<T>(config: MyRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'PATCH' })
  }
}

export default MyRequest
